<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Category_handle extends MY_Controller
{
    function get_tree()
    {
        $this->load->model( 'category_model' );
        if ($this->category_model->load( null, true ))
        {
            $this->rs->ok( $this->category_model->get_data() );
        }
        $this->rs->err( '分类不存在！' );
    }
    function get_subnode()
    {
        $id = ( int ) $this->input->get( 'id' );
        $this->load->model( 'category_model' );
        if ($category = $this->category_model->get( $id ))
        {
            if (empty( $category['fid'] ))
            {
                $this->rs->err( '根分类不允许编辑！' );
            }
            $this->category_model->load( $category['gid'] );
            $category['tree'] = $this->category_model->get_data();
            $this->rs->ok( $category );
        }
        $this->rs->err( '分类不存在！' );
    }
    function delete_subnode()
    {
        $id = ( int ) $this->input->post( 'id' );
        if ($id <= 0)
        {
            $this->rs->err( 2 );
        }
        $this->load->model( 'category_model' );
        $this->category_model->load();
        $current = $this->category_model->get_current( $id );
        if (empty( $current ))
        {
            $this->rs->err( '分类不存在！' );
        }
        if (empty( $current['fid'] ))
        {
            $this->rs->err( '根分类不允许删除！' );
        }
        $modify_data = array (
                'deleted' => 1 
        );
        $ids = $this->category_model->get_ids( $id );
        if (false === $this->category_model->bulk_update( $modify_data, $ids ))
        {
            $this->rs->err( '删除分类及其子分类错误！' );
        }
        $this->load->model( 'content_model' );
        if (false === $this->content_model->bulk_update( $modify_data, $ids, '`cid`' ))
        {
            $this->rs->err( '删除分类下的内容错误！' );
        }
        $this->rs->ok();
    }
    function add_subnode()
    {
        $fid = ( int ) $this->input->post( 'fid' );
        if ($fid <= 0)
        {
            $this->rs->err( 2 );
        }
        $this->load->model( 'category_model' );
        $category = $this->category_model->get( $fid );
        if (empty( $category ))
        {
            $this->rs->err( '父分类不存在！' );
        }
        
        $title = $this->input->post( 'title' );
        if (empty( $title ))
        {
            $title = '新分类名称';
        }
        
        if ($this->category_model->insert( array (
                'fid' => $fid,
                'gid' => $category['gid'],
                'model' => $category['model'],
                'list_uri' => $category['list_uri'],
                'content_uri' => $category['content_uri'],
                'list_tpl' => $category['list_tpl'],
                'content_tpl' => $category['content_tpl'],
                'title' => substr_utf8( $title, 50 ),
                'sort' => 0,
                'deleted' => 0 
        ) ))
        {
            $this->rs->ok();
        }
        $this->rs->err();
    }
    function modify_subnode()
    {
        $id = ( int ) $this->input->post( 'id' );
        if ($id <= 0)
        {
            $this->rs->err( 2 );
        }
        $this->load->model( 'category_model' );
        $category = $this->category_model->get( $id );
        if (empty( $category ))
        {
            $this->rs->err( '分类不存在！' );
        }
        if (empty( $category['fid'] ))
        {
            $this->rs->err( '根分类不允许编辑！' );
        }
        
        $modify_data = array ();
        
        $fid = $this->input->post( 'fid' );
        if (isset( $fid ))
        {
            $fid = ( int ) $fid;
            if ($fid <= 0)
            {
                $this->rs->err( 2 );
            }
            if ($fid == $id)
            {
                $this->rs->err( '上级分类不能为自身！' );
            }
            $category_father = $this->category_model->get( $fid );
            if (empty( $category_father ))
            {
                $this->rs->err( '父分类不存在！' );
            }
            $modify_data['fid'] = $fid;
        }
        
        $title = $this->input->post( 'title' );
        if (isset( $title ))
        {
            if (empty( $title ))
            {
                $this->rs->err( '名称不能为空' );
            }
            $modify_data['title'] = substr_utf8( $title, 50 );
        }
        
        $subtitle = $this->input->post( 'subtitle' );
        if (isset( $subtitle ))
        {
            $modify_data['subtitle'] = substr_utf8( $subtitle, 50 );
        }
        
        $sort = $this->input->post( 'sort' );
        if (isset( $sort ))
        {
            $modify_data['sort'] = ( int ) $sort;
        }
        
        if (empty( $modify_data ))
        {
            $this->rs->err( 2 );
        }
        if (false !== $this->category_model->update( $modify_data, $id ))
        {
            $this->rs->ok();
        }
        $this->rs->err();
    }
    function get_tree_super()
    {
        $this->load->model( 'category_model' );
        if ($this->category_model->load())
        {
            $this->rs->ok( $this->category_model->get_data() );
        }
        $this->rs->err( '分类不存在！' );
    }
    function get_root()
    {
        $id = ( int ) $this->input->get( 'id' );
        $this->load->model( 'category_model' );
        if ($category = $this->category_model->get( $id ))
        {
            $this->rs->ok( $category );
        }
        $this->rs->err( '分类不存在！' );
    }
    function prepare_add_root()
    {
        $this->load->model( 'category_model' );
        $max_gid = $this->category_model->max_gid();
        $this->rs->ok( array (
                'gid' => $max_gid + 1 
        ) );
    }
    function modify_root()
    {
        $id = ( int ) $this->input->post( 'id' );
        $is_insert = $id > 0 ? false : true;
        $modify_data = $category_bulk_data = $content_bulk_data = array ();
        
        $deleted = $this->input->post( 'deleted' );
        if ($is_insert || isset( $deleted ))
        {
            $modify_data['deleted'] = $deleted ? 1 : 0;
            $category_bulk_data['deleted'] = $modify_data['deleted'];
            $content_bulk_data['deleted'] = $modify_data['deleted'];
        }
        
        $title = $this->input->post( 'title' );
        if ($is_insert || isset( $title ))
        {
            if (empty( $title ))
            {
                $this->rs->err( '名称不能为空' );
            }
            $modify_data['title'] = substr_utf8( $title, 50 );
        }
        
        $subtitle = $this->input->post( 'subtitle' );
        if (isset( $subtitle ))
        {
            $modify_data['subtitle'] = substr_utf8( $subtitle, 50 );
        }
        
        $gid = $this->input->post( 'gid' );
        if ($is_insert || isset( $gid ))
        {
            $modify_data['gid'] = ( int ) $gid;
            $category_bulk_data['gid'] = $modify_data['gid'];
            $content_bulk_data['gid'] = $modify_data['gid'];
        }
        
        $model = $this->input->post( 'model' );
        if ($is_insert || isset( $model ))
        {
            $modify_data['model'] = ( int ) $model;
            $category_bulk_data['model'] = $modify_data['model'];
        }
        
        $list_uri = $this->input->post( 'list_uri' );
        if (isset( $list_uri ))
        {
            $modify_data['list_uri'] = substr_utf8( $list_uri, 50 );
            $category_bulk_data['list_uri'] = $modify_data['list_uri'];
        }
        
        $content_uri = $this->input->post( 'content_uri' );
        if ($is_insert || isset( $content_uri ))
        {
            if (empty( $content_uri ))
            {
                $this->rs->err( '内容地址不能为空' );
            }
            $modify_data['content_uri'] = substr_utf8( $content_uri, 50 );
            $category_bulk_data['content_uri'] = $modify_data['content_uri'];
            $content_bulk_data['uri'] = $modify_data['content_uri'];
        }
        
        $list_tpl = $this->input->post( 'list_tpl' );
        if (isset( $list_tpl ))
        {
            $modify_data['list_tpl'] = substr_utf8( $list_tpl, 50 );
            $category_bulk_data['list_tpl'] = $modify_data['list_tpl'];
        }
        
        $content_tpl = $this->input->post( 'content_tpl' );
        if (isset( $content_tpl ))
        {
            $modify_data['content_tpl'] = substr_utf8( $content_tpl, 50 );
            $category_bulk_data['content_tpl'] = $modify_data['content_tpl'];
        }
        
        $sort = $this->input->post( 'sort' );
        if ($is_insert || isset( $sort ))
        {
            $modify_data['sort'] = ( int ) $sort;
        }
        
        if (empty( $modify_data ))
        {
            $this->rs->err( 2 );
        }
        $this->load->model( 'category_model' );
        if ($is_insert)
        {
            if ($this->category_model->check_gid( $modify_data['gid'] ))
            {
                $this->rs->err( '分组已存在！' );
            }
            $modify_data['fid'] = 0;
            if ($this->category_model->insert( $modify_data ))
            {
                $this->rs->ok();
            }
        }
        else
        {
            $category = $this->category_model->get( $id );
            if (empty( $category ))
            {
                $this->rs->err( '分类不存在！' );
            }
            if (! empty( $category['fid'] ))
            {
                $this->rs->err( '不是根分类！' );
            }
            if (isset( $modify_data['gid'] ))
            {
                $id2 = $this->category_model->check_gid( $modify_data['gid'] );
                if (! empty( $id2 ) && $id2 != $id)
                {
                    $this->rs->err( '分组已存在！' );
                }
            }
            if (false === $this->category_model->update( $modify_data, $id ))
            {
                $this->rs->err( '编辑分类错误！' );
            }
            $this->category_model->load();
            $ids = $this->category_model->get_ids( $id );
            if (! empty( $category_bulk_data ))
            {
                if (false === $this->category_model->bulk_update( $category_bulk_data, $ids ))
                {
                    $this->rs->err( '编辑子分类错误！' );
                }
            }
            if (! empty( $content_bulk_data ))
            {
                $this->load->model( 'content_model' );
                if (false === $this->content_model->bulk_update( $content_bulk_data, $ids, '`cid`' ))
                {
                    $this->rs->err( '编辑分类下的内容错误！' );
                }
            }
            $this->rs->ok();
        }
        $this->rs->err();
    }
}